//  ========== 
//  = 产品列表 业务JS = 
//  ==========
var page1 = 1;
var page2 = 1;
var page3 = 1;
var limit = 8;
var Main = function(){
	mui.init({
		swipeBack:true 		//启用右滑关闭功能
	});
	this.init();
	this.proj(0,1);
};
Main.prototype = {

	init : function(){
		mui('.mui-scroll-wrapper').scroll({
					indicators: true //是否显示滚动条
		});
		var _this = this;
		mui.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {
		mui(pullRefreshEl).pullToRefresh({
			down: {
				callback: function() {
				   var self = this;
				   if(index == 0 ){
                       page1=1;					   	
				   }
				   if(index == 1 ){
                       page2=1;	
				   }
				   if(index == 2 ){
                       page3=1;					   	
				   }
					_this.proj(index,1,function(){
						_this._prodBindTap();
						self.endPullDownToRefresh();
					});
				}
			},
			up: {
				callback: function() {
					var self = this;
					var page ;
						if(index == 0 ){
                           ++page1;	
                           page = page1; 
					   }
					   if(index == 1 ){
                           ++page2;		
                           page = page2;
					   }
					   if(index == 2 ){
                           ++page3;
                           page = page3;
					   }
					_this.proj(index,page,function(){
						_this._prodBindTap();
						self.endPullUpToRefresh();
					});

				}
			}
		});
	   });
					
		var _this = this;
		
		_this.eventInit()
		
	},
	eventInit : function(){//绑定事件
		var _this = this;
        $(".mui-control-item").each(function(index){
        	this.addEventListener('tap',function(){
        		page = 1;
        		_this.proj(index,page);
        	});
        });
	},
	_prodBindTap : function(index,param){//列表绑定点击事件,index:0-点点安盈 1-点点专享 2-债券转让
			//点点安盈
			mui(".m-proj").on('tap','li',function(e){
				 var target = $(e.target);
				 var targetClass =  target.attr('class');
				 console.log(targetClass)
				 if(targetClass.indexOf('loan-contract-btn')!=-1 || targetClass.indexOf('m-btn-payment')!=-1){//合同和回款按钮禁止触发
				 	return false;
				 }
				mui.openWindow({
					    url:'bidPlanDetail.html?buyFlag=false&bidId='+this.getAttribute('data-id'),
					    id:'bidPlanDetail',
					    extras:param
				     }
				);
			});
			
		if(index == 0){

		}else if(index == 1){//
			
			$('.m-btn-payment').on('tap',function(){//回款计划
				console.log(111)
				event.stopPropagation()
				var dataId = this.getAttribute('data-id');
				var orderNo = this.getAttribute("data-orderNo");
				var str = dataId+',Financing,'+orderNo;
				var url = 'myBidPlanPayment.html?str='+ str;
				mui.openWindow(url);
			});
		}else if(index == 2){//
			
			$('.m-btn-payment').on('tap',function(){//回款
				event.stopPropagation();
				var dataId = this.getAttribute('data-id');
				var orderNo = this.getAttribute("data-orderNo");
				var str = dataId+',Financing,'+orderNo;
				var url = 'myBidPlanPayment.html?str='+ str;
				mui.openWindow(url);
			});
		}
	},

	//项目列表
	proj : function(index,page,_callback){
		var _this = this;
		var param = {
				isMobile:1,
				memberId:Util.getLoginUser().memberId,
				start:(page-1)*limit,
				limit:8
				};
		var url = server_url+'/financePurchase/myManageMoneyFinancePurchase.do';
		var currTab;
        if(index == 0){//点点安盈 投标中
       	     currTab = $('#item1ul');
       	     param.planstate = 1;
		}else if(index == 1){//点点安盈回款中
			 currTab = $('#item2ul');
			 param.planstate = 7;
		}else if(index == 2){//点点安盈 已还完
			 currTab = $('#item3ul');
			 param.planstate = 10;
		}
		$.getJSON(url,param  ,function(data){
			var _html = '';
            if(data.success){
            	if(index ==0 ){
	            	for(var i=0;i<data.result.length;i++){
	            		var item = data.result[i];
	            		  _html+='\n <!--list beg-->';
	            		  _html+='\n <li class="mui-table-view-cell" data-id="'+item.planId+'">';
	            		  _html+='\n <!--右上角状态 beg-->';
	            		  _html+='\n 	<div class="m-proj-rt-tips">';
	            		  _html+='\n 	<i class="mui-icon u-iconfont">&#xe683;</i>';
	            		  _html+='\n </div>';
	            		  _html+='\n <!--右上角状态 end-->';
	            		  _html+='\n 	<div class="mui-table">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-10">';
	            		  _html+='\n 	<h4 class="mui-ellipsis m-proj-tit">'+item.projectName+'</h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-info">';
	            		  _html+='\n <div class="m-proj-box m-proj-lt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.interestRate+'</span>';
	            		  if(item.addRate && item.addRate > 0 ){
	            		  _html+='\n <span class="m-small">+'+item.addRate+'</span>';	
	            		  }
	            		  _html+='\n <span class="m-unit">%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">年化利率</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-cn">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.loanLife+'</span>';
	            		  /*_html+='\n <span class="m-unit">个月</span>';*/
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">剩余期限</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-rt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.userMoney+'</span>';
	            		  _html+='\n <span class="m-unit">元</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">加入金额</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-range-bar z-act">';
	            		  _html+='\n <i style="width: '+Util.safeString(item.progress,0)+'%;"></i>';
	            		  _html+='\n <div class="m-range-round" style="left: '+Util.safeString(item.progress,0)+'%;">';
	            		  _html+='\n <div class="round"></div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table m-proj-nottom">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5">';
	            		  _html+='\n <h4 class="mui-ellipsis m-proj-tit">';
	            		  _html+='\n 投标日期:<b class="txt">'+item.bidtime+'</b>';
	            		  _html+='\n </h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5 mui-text-right">';
	            		  _html+='\n <span class="mui-h5 m-proj-tag">'+Util.safeString(item.progress,0)+'%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </li>';
	            		  _html+='\n <!-- list end-->';
										
					}	
            	}else if(index == 1 ){
	            	for(var i=0;i<data.result.length;i++){
	            		var item = data.result[i];
	            		  _html+='\n <!--list beg-->';
	            		  _html+='\n <li class="mui-table-view-cell" data-id="'+item.planId+'">';
	            		  _html+='\n <!--右上角状态 beg-->';
	            		  _html+='\n 	<div class="m-proj-rt-tips">';
	            		  _html+='\n 	<i class="mui-icon u-iconfont">&#xe683;</i>';
	            		  _html+='\n </div>';
	            		  _html+='\n <!--右上角状态 end-->';
	            		  _html+='\n 	<div class="mui-table">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-10">';
	            		  _html+='\n 	<h4 class="mui-ellipsis m-proj-tit">'+item.projectName+'</h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-info">';
	            		  _html+='\n <div class="m-proj-box m-proj-lt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.interestRate+'</span>';
	            		  if(item.addRate && item.addRate > 0 ){
	            		  _html+='\n <span class="m-small">+'+item.addRate+'</span>';	
	            		  }
	            		  _html+='\n <span class="m-unit">%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">年化利率</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-cn">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.loanLife+'</span>';
	            		  /*_html+='\n <span class="m-unit">个月</span>';*/
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">剩余期限</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-rt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.userMoney+'</span>';
	            		  _html+='\n <span class="m-unit">元</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">加入金额</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-range-bar z-act">';
	            		  _html+='\n <i style="width: 100%;"></i>';
	            		  _html+='\n <div class="m-range-round" style="left: 100%;">';
	            		  _html+='\n <div class="round"></div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table m-proj-nottom">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5">';
	            		  _html+='\n <h4 class="mui-ellipsis m-proj-tit">';
	            		  _html+='\n 投标日期:<b class="txt">'+item.bidtime+'</b>';
	            		  _html+='\n </h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5 mui-text-right">';
	            		  _html+='\n <span class="mui-h5 m-proj-tag">100%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
		            	  _html += '\n 	<div class="mui-table-cell mui-col-xs-5 mui-text-right m-loan-box mui-clearfix">';
						  _html += '\n 		<!--还款计划-->';
						  _html += '\n 		<input class="m-recharge-btn m-btn-payment" type="button" value="还款计划" data-orderNo="'+item.orderNo+'" data-id="'+item.planId+'"/> ';
						  _html += '\n 		<!--借款合同-->';
						  _html += '\n 		<input class="m-recharge-btn loan-contract-btn" type="button" value="借款合同" data-orderNo="'+item.orderNo+'" data-id="'+item.planId+'"/>';
						  _html += '\n 	</div>';
	            		  _html+='\n </li>';
	            		  _html+='\n <!-- list end-->';
										
					}	
            	}else if(index == 2){
            		
	            	for(var i=0;i<data.result.length;i++){
	            		var item = data.result[i];
	            		  _html+='\n <!--list beg-->';
	            		  _html+='\n <li class="mui-table-view-cell" data-id="'+item.planId+'">';
	            		  _html+='\n <!--右上角状态 beg-->';
	            		  _html+='\n 	<div class="m-proj-rt-tips">';
	            		  _html+='\n 	<i class="mui-icon u-iconfont">&#xe683;</i>';
	            		  _html+='\n </div>';
	            		  _html+='\n <!--右上角状态 end-->';
	            		  _html+='\n 	<div class="mui-table">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-10">';
	            		  _html+='\n 	<h4 class="mui-ellipsis m-proj-tit">'+item.projectName+'</h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-info">';
	            		  _html+='\n <div class="m-proj-box m-proj-lt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.interestRate+'</span>';
	            		  if(item.addRate && item.addRate > 0 ){
	            		  _html+='\n <span class="m-small">+'+item.addRate+'</span>';	
	            		  }
	            		  _html+='\n <span class="m-unit">%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">年化利率</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-cn">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.loanLife+'</span>';
	            		  /*_html+='\n <span class="m-unit">个月</span>';*/
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">剩余期限</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-rt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.userMoney+'</span>';
	            		  _html+='\n <span class="m-unit">元</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">加入金额</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-range-bar z-act">';
	            		  _html+='\n <i style="width: 100%;"></i>';
	            		  _html+='\n <div class="m-range-round" style="left: 100%;">';
	            		  _html+='\n <div class="round"></div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table m-proj-nottom">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5">';
	            		  _html+='\n <h4 class="mui-ellipsis m-proj-tit">';
	            		  _html+='\n 投标日期:<b class="txt">'+item.bidtime+'</b>';
	            		  _html+='\n </h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5 mui-text-right">';
	            		  _html+='\n <span class="mui-h5 m-proj-tag">100%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html += '\n 	<div class="mui-table-cell mui-col-xs-5 mui-text-right m-loan-box mui-clearfix">';
						  _html += '\n 		<!--还款计划-->';
						  _html += '\n 		<input class="m-recharge-btn m-btn-payment" type="button" value="还款计划" data-orderNo="'+item.orderNo+'" data-id="'+item.planId+'"/> ';
						  //_html += '\n 		<!--借款合同-->';
						  _html += '\n 		<input class="m-recharge-btn loan-contract-btn" type="button" value="借款合同" data-orderNo="'+item.orderNo+'" data-id="'+item.planId+'"/>';
						  _html += '\n 	</div>';
	            		  _html+='\n </li>';
	            		  _html+='\n <!-- list end-->';
										
					}	
            	
            		
            	}
            	
            }			
            if(page == 1){
            	//currTab.innerHTML = '';
            	currTab.html('');
            }
            currTab.append(_html);
            if(_this._prodBindTap){
			_this._prodBindTap(index,{});
            }
             if($.isFunction(_callback)){
             	_callback();
             }
		});
	}
	
}
